/** 1
 * it.throw不捕获 会中断程序*/
/*function * read(){
  console.log(1);
  yield 'a';
  console.log(2);
  yield 'b';
  console.log(3);
}

let it = read();
console.log(it.next()); //1 //{ value: 'a', done: false }

//直接抛出错误打印在控制台(如果不捕获的话), 中断程序执行(不会打印下面的console.log('end'))
it.throw('主动触发的错误')
console.log('end');*/

/** 2
 * 仅仅捕获it.throw, 再次调用迭代器是不会往下执行的*/

/*function * read(){
  console.log(1);
  yield 'a';
  console.log(2);
  yield 'b';
  console.log(3);
}

let it = read();
console.log(it.next()); //1 //{ value: 'a', done: false }
//，即使错误被捕获，生成器里后面的代码也都不会再执行了
//，即如果继续it.next()是无效的(但不会影响生成器外的代码执行，比如下面的console.log('end'))
try {
  it.throw('主动触发的错误')
}catch (e){
  console.log('catch e',e); //catch e 主动触发的错误
}
console.log(it.next()); //不会打印*read里的2(即*read里的根本不会走, 直接返回了) //{ value: undefined, done: true }
console.log(it.next()); //不会打印*read里的3(即*read里的根本不会走, 直接返回了) //{ value: undefined, done: true }
console.log('end'); //end*/

/** 3
 * 即使在*read里也进行了try catch
 * 只用it.throw了，那么再次it.next依然不会让迭代器继续执行*/
/*function* read() {
  try {
    console.log(1);
    yield 'a';
    console.log(2);
    yield 'b';
    console.log(3);
  } catch (e) {
    console.log('*read 里的 try catch, e:', e)
  }
  console.log('* read end');
}

let it = read();
try {
  it.throw('主动触发的错误')
}catch (e){
  console.log('catch e',e); //catch e 主动触发的错误
}
console.log(it.next()); //不会打印*read里的2(即*read里的根本不会走, 直接返回了) //{ value: undefined, done: true }
console.log(it.next()); //不会打印*read里的3(即*read里的根本不会走, 直接返回了) //{ value: undefined, done: true }
console.log('end');*/


/** 4
 * 如果直接在生成器里直接it.throw
 * 并且在生成器里try catch捕获的话
 * 是可以被捕获的, 并且生成器里try catch往下的代码也会执行*/
/*const Promise = require('../2.promise/history4【各种方法】/promise')
function* read() {
  try {
    console.log(1);
    yield it.throw('直接在生成器里 it.throw')
    console.log(2);
    yield 'b';
    console.log(3);
  } catch (e) {
    console.log('*read 里的 try catch, e:', e)
  }
  console.log('* read end');
}

let it = read();
it.next();
it.next();
it.next();
console.log('end');*/
/*
1
*read 里的 try catch, e: TypeError: Generator is already running
    at read.throw (<anonymous>)
    at read (F:\js_workplace\tutorial\node_tutorial\3.generator\1.generator.js:74:19)
    at read.next (<anonymous>)
    at Object.<anonymous> (F:\js_workplace\tutorial\node_tutorial\3.generator\1.generator.js:85:4)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
* read end
end
*/

/** 5
 * 或则在生成器里定义一个it.throw包装后的方法 导出去使用
 * 结果也和case4 是一样的*/
/*const Promise = require('../2.promise/history4【各种方法】/promise')
function* read() {
  try {
    console.log(1);
    yield {throwX:()=>{it.throw('直接在生成器里 it.throw')}}
    console.log(2);
    yield 'b';
    console.log(3);
  } catch (e) {
    console.log('*read 里的 try catch, e:', e)
  }
  console.log('* read end');
}

let it = read();
const {value, done} = it.next();
value.throwX();
it.next();
it.next();
console.log('end');*/

/*
1
*read 里的 try catch, e: 直接在生成器里 it.throw
* read end
end
*/
